package com.minimoment.util.ToolAction;

import com.minimoment.util.securitycode.CreateCode;
import com.minimoment.util.securitycode.DrawSecurityCodeImage;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.ByteArrayInputStream;
import java.util.Map;

/**
 * Created by etund on 2014/10/23.
 */
@Controller
@Scope("prototype")
public class SecurityCodeAction extends ActionSupport implements SessionAware {

    private Map<String ,Object> session;

//    声明图片流
    private ByteArrayInputStream imageInputStream;

    public ByteArrayInputStream getImageInputStream() {
        return imageInputStream;
    }

    public void setImageInputStream(ByteArrayInputStream imageInputStream) {
        this.imageInputStream = imageInputStream;
    }

    public String excute() throws  Exception{
//        如果开始Hard就不区分大小写
//        String securityCode = CreateCode.getSecurityCode(4, CreateCode.SecurityCodeLevel.HARD,false).toLowerCase();
            String securityCode = CreateCode.getSecurityCode();
//        String securityCode = "Se12";
        imageInputStream = DrawSecurityCodeImage.getImageAsInputStream(securityCode);
        session.put("SESSION_SECURITY_CODE",securityCode);
        return "success";
    }
    @Override
    public void setSession(Map<String, Object> session) {
            this.session = session;
    }
}
